package com.demo2do.fms.scheduler.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.demo2do.fms.entity.vehicle.source.VehicleSource;
import com.demo2do.fms.entity.vehicle.tractor.Tractor;
import com.demo2do.fms.entity.vehicle.trailer.Trailer;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.entity.vehicle.source.Vehicle;
import com.demo2do.fms.entity.vehicle.source.VehicleGpsInfo;
import com.demo2do.fms.scheduler.dao.fms.VehicleLockDao;
import com.demo2do.fms.scheduler.service.VehicleLockReader;

/**
 *
 * @author Ark
 *
 */
/**
 *
 * @author Ark
 *
 */
@Service("vehicleLockReader")
public class VehicleLockReaderImpl implements VehicleLockReader {

	@Dao
	private VehicleLockDao vehicleLockDao;
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.scheduler.service.VehicleLockReader#listNoDriverVehicle()
	 */
	public List<Vehicle> listNoDriverVehicle() {
		return vehicleLockDao.listNoDriverVehicle();
	}
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.scheduler.service.VehicleLockReader#listVehicleByGps(java.util.Date)
	 */
	@SuppressWarnings("unchecked")
	public List<Long> listVehicleByGps(Date lastWeek) {
		
		List<Long> needLockVids1 = new ArrayList<Long>();
		List<Long> needLockVids2 = new ArrayList<Long>();
		
		// 1. list in 7 day, no gps info vehicle
		List<VehicleGpsInfo> vehicleGpsInfos1 = vehicleLockDao.listNeedLockVehicle(lastWeek);
		if (CollectionUtils.isNotEmpty(vehicleGpsInfos1)) {
			needLockVids1 = com.demo2do.core.utils.CollectionUtils.extractPropertyList(vehicleGpsInfos1, "vid");
		}
				
		// 2. list always no gps info vehicle
		List<VehicleGpsInfo> vehicleGpsInfos2 = vehicleLockDao.listNoGpsVehicle();
		if (CollectionUtils.isNotEmpty(vehicleGpsInfos2)) {
			needLockVids2 = com.demo2do.core.utils.CollectionUtils.extractPropertyList(vehicleGpsInfos2, "vid");
		}
		
		return (List<Long>) CollectionUtils.union(needLockVids1, needLockVids2);
		
	}
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.scheduler.service.VehicleLockReader#listNeedUnlockVehicle(java.util.Date)
	 */
	public List<VehicleGpsInfo> listNeedUnlockVehicle(Date twoHoursBefore) {
		return vehicleLockDao.listNeedUnlockVehicle(twoHoursBefore);
	}

	/**
	 * @param date
	 * @return java.util.List<com.demo2do.fms.entity.vehicle.tractor.Tractor>
	 * @description
	 * @params [date]
	 * @author LZJ
	 * @date 2020/12/1 21:08
	 */
	public List<VehicleSource> listAnnualInspectionDate(Date date) {

		List<VehicleSource> sources = new ArrayList<>();

		//list 整车，分别检查牵引车和挂车年检有效期
		List<VehicleSource> vehicleSources = vehicleLockDao.listUnlockVehicle();
		//满足当下日期大于“年检有效期至”，锁定牵引车和挂车
		for(VehicleSource vehicleSource : vehicleSources){

			String tractorBrandCode = vehicleSource.getTractorBrandCode();
			String trailerBrandCode = vehicleSource.getTrailerBrandCode();

			Tractor tractor = vehicleLockDao.loadTractor(tractorBrandCode);
			Trailer trailer = vehicleLockDao.loadTrailer(trailerBrandCode);

			//校验牵引车和挂车存在，且都有中文车牌
			if(tractor == null || trailer == null){
				continue;
			}

			long tractorTime = tractor.getAnnualInspectionDate().getTime();
			long trailerTime = trailer.getAnnualInspectionDate().getTime();

			if(tractorTime < date.getTime() || trailerTime < date.getTime()){
				sources.add(vehicleSource);
			}
		}
		return sources;
	}
}
